package com.kkb.kkbportal.service.impl;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kkb.kkbcommon.api.ResultCode;
import com.kkb.kkbcommon.exception.Asserts;
import com.kkb.kkbportal.dao.ProjectInfoDao;
import com.kkb.kkbportal.domain.ProjectInfo;
import com.kkb.kkbportal.service.ProjectManagementService;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @ClassName ProjectManagementService
 * @Description 项目管理service
 * @Author 侯兴飞
 * @Date 2021/8/13
 **/
//ServiceImpl<RecommendMapper, Recommend>  继承mp内置的service方法 里面包括依赖注入方式中的泛型注入baseMapper
@Service
public class ProjectManagementServiceImpl extends ServiceImpl<ProjectInfoDao, ProjectInfo> implements ProjectManagementService {
    /**
     * 分页查询
     *
     * @param condition 分页条件对象
     * @param pageNum   当前页码
     * @param pageSize  分页查询条数
     * @return PageInfo<ProjectInfo>
     */
    @Override
    public PageInfo<ProjectInfo> findPage(ProjectInfo condition, int pageNum, int pageSize) {
        Integer status = condition.getStatus();
        String type = condition.getType();
        Date releaseTime = condition.getReleaseTime();
        PageInfo<ProjectInfo> pageInfo = PageHelper.startPage(pageNum, pageSize).doSelectPageInfo(() -> {
            //lambdaquerywrapper的写法
            lambdaQuery().eq(ObjectUtil.isNotNull(status), ProjectInfo::getStatus, status)
                    .eq(ObjectUtil.isNotNull(type), ProjectInfo::getType, type)
                    .lt(ObjectUtil.isNotNull(releaseTime), ProjectInfo::getReleaseTime, releaseTime)
                    .list();
        });
        return pageInfo;
    }

    /**
     * 根据项目id查询已完成的项目
     *
     * @param id
     * @return
     */
    @Override
    public ProjectInfo finshed(Long id) {
        ProjectInfo byId = findById(id);
        if (byId.getStatus() != 2) {
            return null;
        }
        return byId;
    }

    /**
     * 添加项目
     *
     * @param info 项目信息
     * @return void
     */
    @Override
    public void add(ProjectInfo info) {
        int i = baseMapper.insert(info);
        //业务在service写，逻辑不能出现在controller层中
        if (i == 0) {
            //在此处抛异常，不返回数据
            Asserts.fail("上传失败，请检查！");
        }
    }

    /**
     * 根据ID查询项目
     *
     * @param id 项目id
     * @return CommonResult<ProjectInfo>
     */
    @Override
    public ProjectInfo findById(long id) {
        ProjectInfo projectInfo = baseMapper.selectById(id);
        if (projectInfo == null) {
            Asserts.fail(ResultCode.FAILED);
        }
        return projectInfo;
    }

    /**
     * 根据id修改项目信息
     *
     * @param info 项目信息
     * @param id   项目id
     * @return void
     */
    @Override
    public void update(ProjectInfo info, long id) {
        info.setId(id);
        int i = baseMapper.updateById(info);
        if (i == 0) {
            Asserts.fail(ResultCode.FAILED);
        }
    }

    /**
     * 逻辑删除
     *
     * @param id 项目id
     * @return void
     */
    @Override
    public void delete(long id) {
        int delete = baseMapper.deleteById(id);
        if (delete == 0) {
            Asserts.fail(ResultCode.FAILED);
        }
    }


}
